const db_in_theaters = require("./collections/db_in_theaters");
const db_coming_soon = require("./collections/db_coming_soon");
const db_movie_detail = require("./collections/db_movie_detail");

//1.引入相关数据
// let db_movie_detailData = require("./data/db_movie_detail");

//2.插入语句

// db_movie_detail.insertMany(db_movie_detailData, err => {
//     if (!err) {
//         console.log("dt_index_banners插入成功！")
//     }
// })



var express = require('express')
var mongoose = require("mongoose")
var app = express();
app.listen(9000,"0.0.0.0")
var router = express.Router()
app.use("/db",router)

///db/in_theaters?page=1&limit=4
router.get("/in_theaters",(req,res,next)=>{
    let page = parseInt(req.param('page')) || 1 //1
    let pageSize = parseInt(req.param('limit')) || 10
    let sort = req.param("sort")
    let skip = (page-1) * pageSize;
    let params = {};
    let hotsModel = db_in_theaters.find(params).skip(skip).limit(pageSize)
    let len = 0;
    db_in_theaters.find(params,(err,result)=>{
        len = result.length;
        // hotsModel.sort({ 'favorite_count': sort })
        hotsModel.exec((err, doc) => {
            if (err) {
                res.json({
                    status: '-1',
                    msg: err.message
                })
            } else {
                res.json({
                    status: '1',
                    total: len,
                    count: doc.length,
                    object_list: doc
                })
            }
        })
    })
})

///db/coming_soon?page=1&limit=4
router.get("/coming_soon", (req, res, next) => {
    let page = parseInt(req.param('page')) || 1 //1
    let pageSize = parseInt(req.param('limit')) || 10
    let sort = req.param("sort")
    let skip = (page - 1) * pageSize;
    let params = {};
    let hotsModel = db_coming_soon.find(params).skip(skip).limit(pageSize)
    let len = 0;
    db_coming_soon.find(params, (err, result) => {
        len = result.length;
        // hotsModel.sort({ 'favorite_count': sort })
        hotsModel.exec((err, doc) => {
            if (err) {
                res.json({
                    status: '-1',
                    msg: err.message
                })
            } else {
                res.json({
                    status: '1',
                    total: len,
                    count: doc.length,
                    object_list: doc
                })
            }
        })
    })
})





//根据_id查询
router.get("/movie_detail/:id", (req, res, next) => {
    let movieId = req.params.id;
    // goodId = mongoose.Types.ObjectId(goodId);
    let bannersModel = db_movie_detail.find({"id":movieId})
    bannersModel.exec((err, doc) => {
        if (err) {
            res.json({
                status: '-1',
                msg: err.message
            })
        } else {
            res.json(doc[0])
        }
    })
})





// router.get("/home/luxury", (req, res, next) => {
//     // 接受前端传来的参数
//     let page = parseInt(req.param('page')) //1
//     let pageSize = parseInt(req.param('limit'))//3
//     let sort = req.param("sort")
//     let skip = (page - 1) * pageSize//1*3
//     let params = {};
//     let hotsModel = dt_index_hots.find(params).skip(skip).limit(pageSize)
//     let len = 0;
//     dt_index_hots.find(params, (err, result) => {
//         len = result.length;
//         hotsModel.sort({ 'favorite_count': sort })
//         hotsModel.exec((err, doc) => {
//             if (err) {
//                 res.json({
//                     status: '-1',
//                     msg: err.message
//                 })
//             } else {
//                 res.json({
//                     status: '1',
//                     data: {
//                         total: len,
//                         current_total: doc.length,
//                         object_list: doc
//                     }
//                 })
//             }
//         })
//     })
// })



//更新语句
// dt.updateOne({ sender_id: 16617347 }, { $set: { msg:"哈哈哈"}},err=>{
//     if(!err){
//         console.log("更新成功！")
//     }
// })

// dt.updateMany({ name: "王五" }, { $set: { age: 30 } }, err => {
//     if (!err) {
//         console.log("更新成功！")
//     }
// })


//删除语句
// dt.deleteMany({name:"Lisi"},err=>{
//     if(!err){
//         console.log("删除成功！")
//     }
// })


//查找用户
// dt.find({ name: "王五" }, { name: 1, age: 1, _id: 0 }, (err, result) => {
//     if (!err) {
//         var str = ""
//         result.forEach(item => {
//             str += item.name + ":" + item.age + "\n"
//         })
//         console.log("用户信息为：\n" + str)
//     }
// });